package com.RedRiver.hospitalsystem.personal_service.app.controller;


import com.RedRiver.hospitalsystem.personal_service.service.DepartmentService;
import com.RedRiver.hospitalsystem.personal_service.service.Form.DepartmentChangeForm;
import com.RedRiver.hospitalsystem.personal_service.service.Form.DepartmentForm;
import com.RedRiver.hospitalsystem.personal_service.service.Form.PageForm;
import com.RedRiver.hospitalsystem.personal_service.service.info.DepartmentInfo;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@RestController
@RequestMapping("/department")
@Api(tags = "部门管理接口")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation("分页查看所有部门信息")
    @PostMapping("/findDepartment")
    public Result findDepartment(@RequestBody PageForm form){
        IPage<DepartmentInfo> byPage = departmentService.findPageDepartment(form);
        return  Result.getInstance(IPage.class).setMsg("分页成功").setData(byPage);
    }

    @ApiOperation("根据id查询部门名称")
    @PostMapping("/findDepartmentNameById/{departmentId}")
    public Result findDepartmentNameById(@PathVariable("departmentId") Integer departmentId){
        String departmentName = departmentService.getById(departmentId).getDepartmentName();
        if (StringUtils.isEmpty(departmentName)) return Result.getInstance(String.class).setMsg("查询失败").setData(departmentName);
        return Result.getInstance(String.class).setMsg("查询成功").setData(departmentName);
    }

    @ApiOperation("新增部门")
    @PostMapping("/createDepartment")
    public Result createDepartment(@RequestBody DepartmentForm departmentForm){
        return departmentService.createDepartment(departmentForm);
    }

    @ApiOperation("修改部门信息")
    @PostMapping("/changeDepartment")
    public Result changeDepartment(@RequestBody DepartmentChangeForm departmentChangeForm){
        return departmentService.updateDepartment(departmentChangeForm);
    }


    @ApiOperation("删除部门")
    @PostMapping("/delDepartment/{departmentId}")
    public Result delDepartment(@PathVariable("departmentId") Integer departmentId){
        return departmentService.delDepartment(departmentId);
    }

}

